/*
This file compares recall data on monthly food security from the post-COVID
survey to comparable in-person survey data collected one year prior.
*/

use "$da/NPL_recall_data.dta", clear

********************************************************************************
*		Plot Settings
********************************************************************************
* Macro settings for the graph
gl grset graphregion(color(white)) ylabel(, angle(0)) plotregion(margin(sides)) 
graph set window fontface "Helvetica"

gl start = date("2 Jul 2019", "DMY")
gl lstart = date("1 Aug 2019", "DMY")
gl end = date("20 Jun 2020", "DMY")
gl ricestart = date("11 Oct 2019", "DMY")
gl riceend = date("15 Nov 2019", "DMY")
gl wheatstart = date("8 Apr 2020", "DMY")
gl wheatend = date("15 May 2020", "DMY")
gl ct = date("10 March 2020", "DMY")

gl crosshatch xline(${lstart}(1)${ricestart}, lpattern(dot) lcolor("230 159 0%15") lwidth(thick)) 
gl crosshatch ${crosshatch}	xline(${lstart}(1)${ricestart}, lcolor("230 159 0%15") lwidth(vthin)) 			
	
gl xformat	xlabel(${start}(30.75)${end}, angle(45) format(%tdm) ) 
gl xformat ${xformat} ylabel(, format(%9.0g) )

/*Note:	recall denotes whether the sample is the 160 household surveyed in 
			Spring of 2019 or the main study sample surveyed in April-May of 2020
		
		"date" does not refer to the survey date, but the midpoint of the month 
			asked about in the survey, transferred to a common year for plotting
		
		___foodIns_r and ___foodIns are inverse-covariance-weighted indices of the 
			same food insecurity items for the 2020 and 2019 samples, respectively */

********************************************************************************
*		SELF CONTAINED CODE BELOW HERE
********************************************************************************
			
replace round = mod(month,12)+1 if recall == 1

rename (___foodIns ___foodIns_r) (c r)

//encode hhid, gen(hhid_s)
//xtset hhid_s

* Get means and standard errors
foreach vbl of varlist c r {
    areg `vbl' i.round, a(hhid) vce(cluster hhid)
    predict b_`vbl' if !missing( `vbl' ), xb
    predict se_`vbl' if !missing( `vbl' ), stdp
}

collapse (mean) b_* se_* (min) date, by(round) fast
sort date

foreach vbl in c r {
    gen hi_`vbl' = b_`vbl'  + 1.96*se_`vbl'
    gen lo_`vbl' = b_`vbl' - 1.96*se_`vbl'
}

*Standardize both series to have mean zero and sd 1 August-Jan
foreach s in _c _r {
	sum b`s' if date < date("Jan 1, 2020", "MDY") & date > date("Aug 1 2019", "MDY")
	foreach i in b lo hi{
		replace `i'`s' = (`i'`s' - `r(mean)')/`r(sd)'
	}
}

* Duplicate June at start and end
expand 2 if round==3, gen(newyr)
replace date = date+365 if newyr
drop newyr

* Graph it
sort date
format date %td

loc cis_r (rarea lo_r hi_r date , color(gs8) lwidth(0) msize(vtiny))
loc cis_c (rarea lo_c hi_c date , color(gs13) lwidth(0) msize(vtiny))

gl h0 -1
gl h3 2 
gl h1 = ${h3} - 0.05*(${h3}-${h0})
gl h2 = ${h3} - 0.02*(${h3}-${h0})


tw  (scatteri ${ricestart} ${h0}  ${ricestart} ${h3}   ${riceend} ${h3} ${riceend} ${h0}   , ///
		recast(area) horizontal color("0 158 115%15") lwidth(none)) ///
	`cis_c' `cis_r' (line b_r date, color(gs0) sort) ///
	(line b_c date, color(gs0) lpattern(dash) sort) , ///
		 ytitle(Food Security Index during Pre-COVID Period) ${crosshatch} ${xformat}  ///
	legend(order(	4 "Recall about Pre-COVID Collected during COVID Period" ///
					5 "Recall about Pre-COVID Collected before COVID Period") rows(2)) ///
	$grset ///
	ttext(${h1} 1Sept2019 "Lean" "Season" )  ///
	ttext(${h1} 30Oct2019 "Rice" "Harvest")  ///
	yscale(range(${h0} ${h3}) titlegap(0)) ///
	  plotregion(margin(0 0 0 0))

graph export "$dfig/recall_NPL.png", replace
